<template>
  <view class="container">
    <!-- 电站基本信息 -->
    <view class="card">
      <view class="card-header">📍 电站信息</view>
      <view class="card-body">
		  <view>电站编码：{{ station.stationno }}</view>
		  <view>业主姓名：{{ station.guestname }}</view>
		  <view>业主手机：{{ station.phone }}</view>
		<view>装机容量：{{ station.totalpower/1000 }}KW</view>
        <view>地址：{{ station.addressstring }}</view>
        <view class="btn-row">
          <button type="primary" size="mini" @click="navigateToLocation">导航</button>
		  <button style="margin-left: 20rpx;" type="primary" size="mini" @click="callguest">联系业主</button>	
		</view>
		<view class="photo-section">
		  <view class="photo-title">🏠 电站门头照片：</view>
		  <view class="img-list">
			<image
			  v-for="(img, idx) in station.gatePicUrls"
			  :key="idx"
			  :src="img"
			  class="preview-img"
			  mode="aspectFill"
			  @click="previewImage(station.gatePicUrls, idx)"
			/>
		  </view>
		</view>

		<view class="photo-section">
		  <view class="photo-title">📸 电站俯视图：</view>
		  <view class="img-list">
			<image
			  v-for="(img, idx) in station.comPicUrls"
			  :key="idx"
			  :src="img"
			  class="preview-img"
			  mode="aspectFill"
			  @click="previewImage(station.comPicUrls, idx)"
			/>
		  </view>
		</view>

		<view class="photo-section">
		  <view class="photo-title">🧭 周围15米方位图：</view>
		  <view class="img-list">
			<image
			  v-for="(img, idx) in station.locationPicUrls"
			  :key="idx"
			  :src="img"
			  class="preview-img"
			  mode="aspectFill"
			  @click="previewImage(station.locationPicUrls, idx)"
			/>
		  </view>
		</view>
		<view class="photo-section">
		  <view class="photo-title">⚡ 逆变器照片：</view>
		  <view class="img-list">
			<image
			  v-for="(img, idx) in station.invPicUrls"
			  :key="idx"
			  :src="img"
			  class="preview-img"
			  mode="aspectFill"
			  @click="previewImage(station.invPicUrls, idx)"
			/>
		  </view>
		</view>
		<view class="photo-section">
		  <view class="photo-title">📡 采集器照片：</view>
		  <view class="img-list">
			<image
			  v-for="(img, idx) in station.colPicUrls"
			  :key="idx"
			  :src="img"
			  class="preview-img"
			  mode="aspectFill"
			  @click="previewImage(station.colPicUrls, idx)"
			/>
		  </view>
		</view>

      </view>
    </view>

    <!-- 运维人员信息 -->
    <view class="card">
      <view class="card-header">‍🔧 运维信息</view>
      <view class="card-body">
		  <view class="btn-row">
		    <button type="primary" size="mini" @click="onCallClick">📞拨号</button>
		  </view>
		<view>单据类型：<dict-label dict-type="oper_station_form_type" :value="fault.type" /></view>
		<view>本单金额：{{ fault.price }}元</view>
        <view>运维原因：{{ fault.description }}</view>
        <view>开始时间：{{ parseTime(fault.createTime) }}</view>
        <view>当前状态：<dict-label dict-type="oper_fault_status" :value="fault.status" /></view>
        <view>当前节点：<dict-label dict-type="oper_fault_node" :value="fault.node" /></view>

        <view class="photo-section">
          <view class="photo-title">故障照片：</view>
          <view class="img-list">
        	<image
        	  v-for="(img, idx) in fault.picUrls"
        	  :key="idx"
        	  :src="img"
        	  class="preview-img"
        	  mode="aspectFill"
        	  @click="previewImage(fault.picUrls, idx)"
        	/>
          </view>
        </view>
      </view>
    </view>
	
	<view class="card">
	  <view class="card-header">‍📜 订单记录</view>
	  <view class="card-body">
	  <view class="btn-row">
		<button type="primary" size="mini" @tap="showRecord = !showRecord">记录</button>
	  </view>
	    <view
	      class="record-item"
	      v-for="(item, index) in approverList"
	      :key="item.id"
		  v-if="showRecord"
	    >
	      <view class="record-top">
	        <view class="approver"><dict-label dict-type="oper_fault_node" :value="item.node" /></view>
	        <text
	          class="result"
	          :class="item.approveStatus === 1 ? 'pass' : 'reject'"
	        >
	          {{ item.approveResult }}
	        </text>
	      </view>
	      <view class="record-middle">
	        {{ item.suggestion }}
	      </view>
	      <view class="record-bottom">
	        {{ parseTime(item.approveTime) }}
	      </view>
	    </view>
	  </view>
	</view>
	<!-- 不通过原因 -->
	<view class="card" v-if="fault.status == 6">
	  <view class="card-header">❌ 不通过原因</view>
	  {{ fault.approveSuggestion }}
	</view>
	
	
	<!-- 巡检 -->
	<view class="card" v-if="fault.node >= 41">
	  <view class="card-header">📍 电站定位</view>
	
	  <!-- 电站状态 -->
	  <view class="form-row" v-if="fault.node >= 41">
	    <view class="form-label">定位状态：</view>
		<dict-label dict-type="oper_station_location_status" :value="station.locationStatus" />
	  </view>
	   <view class="btn-row" v-if="station.longitude == null || station.latitude == null">
		   <button type="primary" size="mini" @click="navigateToStation(station.id)">更新电站信息</button>
	   </view>
	</view>
	
	
	<!-- 开始巡检信息 -->
	<view class="card" v-if="fault.node >= 42 && fault.type == 'XJ'">
	  <view class="card-header">🛠️ 巡检信息</view>
	  <!-- 照片 -->
	  <view class="form-row">
	    <view class="form-label">组件照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.comPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 42 && fault.status != 6"
	      :disabled="fault.node > 42 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'comPicUrls')" 
	    />
	  </view>
	  <view class="form-row">
	    <view class="form-label">组件背面照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.comBackPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 42 && fault.status != 6"
	      :disabled="fault.node > 42 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'comBackPicUrls')" 
	    />
	  </view>
	  <view class="form-row">
	    <view class="form-label">支架照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.zhijiaPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 42 && fault.status != 6"
	      :disabled="fault.node > 42 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'zhijiaPicUrls')" 
	    />
	  </view>
	  <view class="form-row">
	    <view class="form-label">逆变器照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.invPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 42 && fault.status != 6"
	      :disabled="fault.node > 42 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'invPicUrls')" 
	    />
	  </view>
	  <view class="form-row">
	    <view class="form-label">交流配电箱照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.boxPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 42 && fault.status != 6"
	      :disabled="fault.node > 42 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'boxPicUrls')" 
	    />
	  </view>
	  <view class="form-row">
	    <view class="form-label">电表照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.elePicUrls" 
	      mode="grid"
	      :readonly="fault.node > 42 && fault.status != 6"
	      :disabled="fault.node > 42 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'elePicUrls')" 
	    />
	  </view>
	  <view class="form-row">
	    <view class="form-label">温度照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.tempPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 42 && fault.status != 6"
	      :disabled="fault.node > 42 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'tempPicUrls')" 
	    />
	  </view>
	  <view class="form-row">
	    <view class="form-label">接地电阻照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.resiPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 42 && fault.status != 6"
	      :disabled="fault.node > 42 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'resiPicUrls')" 
	    />
	  </view>
	  
	  <!-- 巡检信息 -->
	  <view class="form-row">
	    <view class="form-label">是否存在故障：</view>
	    <picker 
	      :range="hasOptions" 
	      range-key="label" 
	      @change="(e) => onPickerChange(e, 'has')" 
	      :disabled="fault.node > 42 && fault.status != 6"
	    >
	      <view class="picker-box">
	        {{ hasLabel || '请选择是否存在故障' }}
	      </view>
	    </picker>
	  </view>
	  <view class="form-row" v-if="'是' == hasLabel">
	    <view class="form-label">故障问题：</view>
	    <textarea
	      v-model="fault.faultDesc" 
	      placeholder="请详细填写故障问题" 
	      class="text-area" 
	      :disabled="fault.node > 42 && fault.status != 6"
	    />
	  </view>
	  <view class="form-row" v-if="'是' == hasLabel">
	    <view class="form-label">故障照片：</view>
	      <uni-file-picker 
	        limit="9" 
	        file-mediatype="image" 
	        v-model="fault.beforePicUrls" 
	        mode="grid"
	        :readonly="fault.node > 42 && fault.status != 6"
	        :disabled="fault.node > 42 && fault.status != 6"
	        :imageStyles="imageStyle"
	        @select="(e) => handleImageSelect(e, 'beforePicUrls')" 
	      />
	  </view>
	</view>
	

    <!-- 开始维修照片 -->
   <view class="card" v-if="fault.node >= 50 && (fault.type == 'FA' ||fault.type == 'SEC' ||fault.type == 'INS' ||fault.type == 'ZX')">
     <view class="card-header">🛠️ 故障处理</view>
	 
     <!-- 故障等级 -->
     <view class="form-row">
       <view class="form-label">故障等级：</view>
       <picker 
         :range="levelOptions" 
         range-key="label" 
         @change="(e) => onPickerChange(e, 'level')" 
         :disabled="fault.node > 50 && fault.status != 6"
       >
         <view class="picker-box">
           {{ levelLabel || '请选择故障等级' }}
         </view>
       </picker>
     </view>
   
     <!-- 维修前照片 -->
     <view class="form-row">
       <view class="form-label">维修前照片：</view>
       <uni-file-picker 
         limit="9" 
         file-mediatype="image" 
         v-model="fault.beforePicUrls" 
         mode="grid"
         :readonly="fault.node > 50 && fault.status != 6"
         :disabled="fault.node > 50 && fault.status != 6"
         :imageStyles="imageStyle"
         @select="(e) => handleImageSelect(e, 'beforePicUrls')" 
       />
     </view>
	 
	 <view class="btn-row" v-if="station.longitude == null || station.latitude == null">
	 	 <button type="primary" size="mini" @click="navigateToStation(station.id)">更新电站信息</button>
	 </view>
   </view>
	
	<!-- 维修完成照片 -->
	<view class="card" v-if="fault.node >= 60  && (fault.type == 'FA' ||fault.type == 'SEC' ||fault.type == 'INS' ||fault.type == 'ZX')"> 
	  <view class="card-header">📝 故障反馈</view>
	
	  <!-- 故障类型 -->
	  <view class="form-row" v-if = "fault.type == 'FA' ||fault.type == 'SEC' ||fault.type == 'INS' ||fault.type == 'ZX'">
	    <view class="form-label">故障类型：</view>
	    <picker 
	      :range="typeOptions" 
	      range-key="label" 
	      @change="(e) => onPickerChange(e, 'type')" 
	      :disabled="fault.node > 60 && fault.status != 6"
	    >
	      <view class="picker-box">
	        {{ typeLabel || '请选择故障类型' }}
	      </view>
	    </picker>
	  </view>
	  <view class="form-row" v-if="fault.faultType == 2">
		  
	    <view class="form-label">更换设备：</view>
	    <uni-data-checkbox
	      v-model="faultDeviceChange"
	      :localdata="deviceOptions"
	      multiple
	      selectedColor="#007aff"
		  :disabled="fault.node > 60 && fault.status != 6"
	    />
	  </view>
	
	  <!-- 故障问题描述 -->
	  <view>
	    <view class="form-label">故障问题：</view>
	    <textarea 
	      v-model="fault.faultDesc" 
	      placeholder="请详细填写故障问题" 
	      class="text-area" 
	      :disabled="fault.node > 60 && fault.status != 6"
	    />
	  </view>
	
	  <!-- 维修后照片 -->
	  <view class="form-row">
	    <view class="form-label">维修后照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.afterPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 60 && fault.status != 6"
	      :disabled="fault.node > 60 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'afterPicUrls')" 
	    />
	  </view>
	  
	  <!-- 逆变器指示灯照片 -->
	  <view class="form-row" v-if="fault.type == 'SEC'">
	    <view class="form-label">逆变器指示灯照片：</view>
	    <uni-file-picker 
	      limit="9" 
	      file-mediatype="image" 
	      v-model="fault.lightPicUrls" 
	      mode="grid"
	      :readonly="fault.node > 60 && fault.status != 6"
	      :disabled="fault.node > 60 && fault.status != 6"
	      :imageStyles="imageStyle"
	      @select="(e) => handleImageSelect(e, 'lightPicUrls')" 
	    />
	  </view>
	</view>

    <!-- 提交按钮 -->
    <view class="submit-btn-wrapper" v-if="fault.status != 6">
	  <button v-if="fault.node == 20" type="primary" @click="take">接单</button>
      <button v-if="fault.node == 30" type="primary" @click="visit">出发上门</button>
	  <button v-if="fault.node == 40" type="primary" @click="arrived">已到达</button>
	  <button v-if="fault.node == 41" type="primary" @click="inspection">开始巡检</button>
	  <button v-if="fault.node == 42" type="primary" @click="inspectionEnd">巡检完成</button>
	  <button v-if="fault.node == 50" type="primary" @click="repair">开始维修</button>
	  <button v-if="fault.node == 60" type="primary" @click="finished">维修完成</button>
	</view>
	<view class="submit-btn-wrapper" v-if="fault.status == 6">
		<button type="primary" @click="resend">提交</button>		
	</view>
  </view>
</template>

<script>
import * as faultApi from '@/api/oper/fault/fault.js';
import {parseTime,formatPicUrls} from '@/utils/ruoyi.js';
import dictUtil from '@/utils/dictUtil'
import * as UploadApi from "@/api/infra/upload.js";
import config from '@/config'


	
	
export default {
  data() {
    return {
      station: {       
      },
      fault: {
      },
	  faultDeviceChange : [],
	  approverList:[
		  
	  ],
	  showRecord: false, // 默认不展示
	  phoneOptions : [
	    
	  ],
	  levelOptions : [
		  
	  ],
	  levelLabel : '',
	  typeOptions : [
	  		  
	  ],
	  typeLabel : '',
	  hasOptions : [
	  		  
	  ],
	  hasLabel : '',
	  deviceOptions: [
	  ],
      beforePicUrls: [],
	  afterPicUrls: [],
	  comPicUrls: [],
	  comBackPicUrls: [],
	  zhijiaPicUrls: [],
	  invPicUrls: [],
	  boxPicUrls: [],
	  elePicUrls : [],
	  tempPicUrls: [],
	  resiPicUrls: [],
	  lightPicUrls: [],
      supplement: '',
	  imageStyle: {
	  	width: 100,
	  	height: 100,
	  	border: {
	  		radius: '8px'
	  	}
	  },
    };
  },
  async onLoad(options) {
    console.log(options)
	if (!options.orderId) {
	  uni.showToast({ title: '缺少运维单ID', icon: 'none' })
	  return
	}
  
	const orderId = Number(decodeURIComponent(options.orderId))
  
	// ✅ 等待加载数据完成
	await this.loadStationAndFault(orderId)
  
	// 📌 确保 this.fault 数据已经准备好
	const faultLevel = this.fault.faultLevel
	const faultType = this.fault.faultType
	const faultHas = this.fault.faultHas
  
	// 📌 加载运维等级字典
	const levelRes = await dictUtil.getDictData('oper_fault_level')
	this.levelOptions = levelRes.map(item => ({
	  label: item.label,
	  value: item.value
	}))
	const levelMatch = this.levelOptions.find(item => item.value == faultLevel)
	this.levelLabel = levelMatch ? levelMatch.label : ''
  
	// 📌 加载运维类型字典
	const typeRes = await dictUtil.getDictData('oper_fault_type')
	this.typeOptions = typeRes.map(item => ({
	  label: item.label,
	  value: item.value
	}))
	const typeMatch = this.typeOptions.find(item => item.value == faultType)
	this.typeLabel = typeMatch ? typeMatch.label : ''
  
	// 📌 加载电话选项
	const phoneRes = await dictUtil.getDictData('oper_phone')
	this.phoneOptions = phoneRes.map(item => ({
	  label: item.label,
	  value: item.value
	}))
  
	// 📌 加载设备类型选项
	const deviceRes = await dictUtil.getDictData('oper_station_device_type')
	this.deviceOptions = deviceRes.map(item => ({
	  text: item.label,
	  value: item.value.toString()
	}))
	  
	// 📌 加载是否类型选项
	const hasRes = await dictUtil.getDictData('infra_boolean_string')
	this.hasOptions = hasRes.map(item => ({
	  label: item.label,
	  value: item.value
	}))

  	const hasMatch = this.hasOptions.find(item => item.value == faultHas)
	this.hasLabel = hasMatch ? hasMatch.label : ''
	  
	  
  },
  methods: {
	  parseTime,
	 async loadStationAndFault(orderId){
		 const res = await faultApi.getFaultAndStation(orderId);
		 this.station = res.data.station;
		 this.fault = res.data;
		 this.approverList = res.data.approverList;
		 //图片处理
		 this.fault.beforePicUrls = formatPicUrls(this.fault.beforePicUrls);
		 this.fault.afterPicUrls = formatPicUrls(this.fault.afterPicUrls);
		 this.fault.comPicUrls = formatPicUrls(this.fault.comPicUrls);
		 this.fault.comBackPicUrls = formatPicUrls(this.fault.comBackPicUrls);
		 this.fault.zhijiaPicUrls = formatPicUrls(this.fault.zhijiaPicUrls);
		 this.fault.invPicUrls = formatPicUrls(this.fault.invPicUrls);
		 this.fault.boxPicUrls = formatPicUrls(this.fault.boxPicUrls);
		 this.fault.elePicUrls = formatPicUrls(this.fault.elePicUrls);
		 this.fault.tempPicUrls = formatPicUrls(this.fault.tempPicUrls);
		 this.fault.resiPicUrls = formatPicUrls(this.fault.resiPicUrls);
		 this.fault.lightPicUrls = formatPicUrls(this.fault.lightPicUrls);
					
		 //多选框处理
		 if (this.fault && this.fault.faultDeviceChange) {
		   this.faultDeviceChange = this.fault.faultDeviceChange;
		 } else {
		   this.faultDeviceChange = [];
		 }
		 console.log(this.faultDeviceChange); 
		 this.$forceUpdate() // 强制刷新
	} ,
	navigateToStation(stationId){
      uni.navigateTo({ url: `/pages/work/stationNew/detail?stationId=${stationId}` });
	},
    navigateToLocation() {
	  console.log(this.station);
      uni.openLocation({
        latitude: Number(this.station.latitude),
        longitude: Number(this.station.longitude),
        name: this.station.name,
        address: this.station.addressstring,
        scale: 18
      });
    },
    onCallClick() {
        if (!this.phoneOptions || this.phoneOptions.length === 0) {
          uni.showToast({ title: '暂无可拨打电话', icon: 'none' });
          return;
        }
    
        // 弹出选项列表
        const phoneLabels = this.phoneOptions.map(item => item.label + '（' + item.value + '）');
    
        uni.showActionSheet({
          itemList: phoneLabels,
          success: res => {
            const selectedPhone = this.phoneOptions[res.tapIndex].value;
            // 拨号
            uni.makePhoneCall({ phoneNumber: selectedPhone });
          },
          fail: err => {
            console.log('用户取消拨号选择', err);
          }
        });
      },
	callguest(){
		if(!this.station.phone || "null" == this.station.phone){
			uni.showToast({ title: '暂无可拨打电话', icon: 'none' });
			return;
		}
		uni.makePhoneCall({ phoneNumber: this.station.phone });
	} ,
	  
    handleImageSelect(e, type) {
    	console.log(type);
	    const files = e.tempFiles || [];
	    if (files.length === 0) return;
		
		files.forEach(file => {
		    UploadApi.uploadImage(file.path, type, (url) => {
		      if (!this.fault[type]) {
		        this.$set(this.fault, type, []);
		      }
		      this.fault[type].push({
		        url: url,
		        extname: url.split('.').pop().toLowerCase()
		      });
		    });
		});
		    },
    previewImage(list, index) {
      uni.previewImage({
        urls: list,
        current: list[index]
      });
    },
	onPickerChange(e, key) {
	  const index = e.detail.value;
	  const optionsKey = key + 'Options';
	  const labelKey = key + 'Label';
	  const valueKey = 'fault' + key.charAt(0).toUpperCase() + key.slice(1);

	  const item = this[optionsKey][index];
	  this.fault[valueKey] = item.value;
	  this[labelKey] = item.label;
	},
	
	async take(){
		const faultId = this.fault.id;
		uni.showLoading({ title: '正在提交...' }); 
		const res = await faultApi.take(faultId);
		uni.hideLoading(); // 👉 无论成功或失败都隐藏加载提示
		if (res.code === 0) {
		  uni.showToast({ title: '操作成功', icon: 'success' });		 
		} else {
		  uni.showToast({ title: res.message || '操作失败', icon: 'none' });
		}
		this.loadStationAndFault(Number(faultId));
	},
	
	async visit() {
		const faultId = this.fault.id;
			
		uni.showLoading({ title: '定位中...' }); 
		uni.getLocation({
			type: 'gcj02',
			isHighAccuracy:true,
			highAccuracyExpireTime:4000,
		  success: (res) => {
		    this.visitLoc(res.latitude,res.longitude);
		  },
		  fail: () => {	    
			//测试环境模直接模拟经纬度进行写入
			if (config.baseUrl.includes('localhost')) {
				this.visitLoc(1,1);
			}else{
				uni.showToast({ title: '定位失败', icon: 'none' });
			}
		  },
		  complete : () => {
			  uni.hideLoading(); // 👉 无论成功或失败都隐藏加载提示
		  }
		});		
	},
	
	async visitLoc(latitude,longitude) {
	  const faultId = this.fault.id;
	  const params = {
	    id: faultId,
	    startLatitude: latitude,
	    startLongitude: longitude
	  };
	  uni.showLoading({ title: '正在提交...' });
	  const result = await faultApi.visit(params); // 这个 await 也没问题
	  uni.hideLoading(); 
	  if (result.code === 0) {
	    uni.showToast({ title: '操作成功', icon: 'success' });
	  } else {
	    uni.showToast({ title: result.message || '操作失败', icon: 'none' });
	  }
	  this.loadStationAndFault(Number(faultId));
	},
	
	async arrivedLoc(latitude,longitude) {
		const faultId = this.fault.id;
		const params = {
		  id: faultId,
		  endLatitude: latitude,
		  endLongitude: longitude
		};
		uni.showLoading({ title: '正在提交...' });
		const result = await faultApi.arrived(params); // 这个 await 也没问题
		uni.hideLoading(); 	
		if (result.code === 0) {
		  uni.showToast({ title: '操作成功', icon: 'success' });
		} else {
		  uni.showToast({ title: result.message || '操作失败', icon: 'none' });
		}
		this.loadStationAndFault(Number(faultId));
	},
	
	async arrived() {
	  const faultId = this.fault.id;
	  uni.showLoading({ title: '定位中...' }); // 👉 显示加载中提示
	  uni.getLocation({
	    type: 'gcj02',
		isHighAccuracy:true,
		highAccuracyExpireTime:4000,
	    success: (res) => {
	      this.arrivedLoc(res.latitude,res.longitude);
	    },
	    fail: () => {
	      
			//测试环境模直接模拟经纬度进行写入
			if (config.baseUrl.includes('localhost')) {
				this.arrivedLoc(1,1);
			}else{
				uni.showToast({ title: '定位失败', icon: 'none' });
			}
	    },complete : () => {
		  uni.hideLoading(); // 👉 无论成功或失败都隐藏加载提示
	    }
	  });
	},
	
	async inspection() {

	    const faultId = this.fault.id;
		const params = {
		  id: faultId
		};
		uni.showLoading({ title: '正在提交...' });
		const result = await faultApi.inspection(params); 
		uni.hideLoading(); 
		if (result.code === 0) {
		  uni.showToast({ title: '操作成功', icon: 'success' });
		} else {
		  uni.showToast({ title: result.message || '操作失败', icon: 'none' });
		}
		this.loadStationAndFault(Number(faultId));
	},
	
	async inspectionEnd() {
		if(!this.fault.comPicUrls || this.fault.comPicUrls.length === 0){
			return uni.showToast({ title: '请上传组件照片', icon: 'none' });
		}
		if(!this.fault.comBackPicUrls || this.fault.comBackPicUrls.length === 0){
			return uni.showToast({ title: '请上传组件背面照片', icon: 'none' });
		}
		if(!this.fault.zhijiaPicUrls || this.fault.zhijiaPicUrls.length === 0){
			return uni.showToast({ title: '请上传支架照片', icon: 'none' });
		}
		if(!this.fault.invPicUrls || this.fault.invPicUrls.length === 0){
			return uni.showToast({ title: '请上传逆变器照片', icon: 'none' });
		}
		if(!this.fault.boxPicUrls || this.fault.boxPicUrls.length === 0){
			return uni.showToast({ title: '请上传交流配电箱照片', icon: 'none' });
		}
		if(!this.fault.elePicUrls || this.fault.elePicUrls.length === 0){
			return uni.showToast({ title: '请上传电表照片', icon: 'none' });
		}
		if(!this.fault.tempPicUrls || this.fault.tempPicUrls.length === 0){
			return uni.showToast({ title: '请上传温度照片', icon: 'none' });
		}
		
		if (!this.fault.faultHas) {
		  return uni.showToast({ title: '请选择是否存在故障', icon: 'none' });
		}
		if (this.fault.faultHas == 'true' && !this.fault.faultDesc ) {
		  return uni.showToast({ title: '请详细描述故障', icon: 'none' });
		}
		
		if (this.fault.faultHas == 'true'  && (!this.fault.beforePicUrls || this.fault.beforePicUrls.length === 0) ) {
		  return uni.showToast({ title: '请上传故障照片', icon: 'none' });
		}
		

		
	    const faultId = this.fault.id;
		const params = {
		  id: faultId,
		  faultHas : this.fault.faultHas,
		  faultDesc : this.fault.faultDesc,
		  comPicUrls : this.fault.comPicUrls?.map(item => item.url),
		  comBackPicUrls : this.fault.comBackPicUrls?.map(item => item.url),
		  zhijiaPicUrls : this.fault.zhijiaPicUrls?.map(item => item.url),
		  invPicUrls : this.fault.invPicUrls?.map(item => item.url),
		  boxPicUrls : this.fault.boxPicUrls?.map(item => item.url),
		  elePicUrls : this.fault.elePicUrls?.map(item => item.url),
		  tempPicUrls : this.fault.tempPicUrls?.map(item => item.url),
		  resiPicUrls : this.fault.resiPicUrls?.map(item => item.url),
		  beforePicUrls : this.fault.beforePicUrls?.map(item => item.url),
		};
		uni.showLoading({ title: '正在提交...' });
		const result = await faultApi.inspectionEnd(params); // 这个 await 也没问题
		uni.hideLoading(); 
		if (result.code === 0) {
		  uni.showToast({ title: '操作成功', icon: 'success' });
		} else {
		  uni.showToast({ title: result.message || '操作失败', icon: 'none' });
		}
		this.loadStationAndFault(Number(faultId));
	},
	
	async repair() {
		if (!this.fault.faultLevel) {
		  return uni.showToast({ title: '请选择故障等级', icon: 'none' });
		}
		
		if(!this.fault.beforePicUrls || this.fault.beforePicUrls.length === 0){
			return uni.showToast({ title: '请上传维修前照片', icon: 'none' });
		}
		
	    const faultId = this.fault.id;
		const params = {
		  id: faultId,
		  faultLevel : this.fault.faultLevel,
		  beforePicUrls : this.fault.beforePicUrls?.map(item => item.url),
		};
		uni.showLoading({ title: '正在提交...' });
		const result = await faultApi.repair(params); // 这个 await 也没问题
		uni.hideLoading(); 
		if (result.code === 0) {
		  uni.showToast({ title: '操作成功', icon: 'success' });
		} else {
		  uni.showToast({ title: result.message || '操作失败', icon: 'none' });
		}
		this.loadStationAndFault(Number(faultId));
	},
	
	async finished() {
		if (!this.fault.faultType) {
		  return uni.showToast({ title: '请选择故障类型', icon: 'none' });
		}
		
		if (this.fault.faultType == 2 ) {
		  // 已经选中
		  if (!Array.isArray(this.faultDeviceChange) || this.faultDeviceChange.length === 0) {
		    return uni.showToast({ title: '请选择更换设备', icon: 'none' });
		  }
		}
		
		if (!this.fault.faultDesc) {
		  return uni.showToast({ title: '请填写故障问题', icon: 'none' });
		}
		
		if(!this.fault.afterPicUrls || this.fault.afterPicUrls.length === 0){
			return uni.showToast({ title: '请上传维修后照片', icon: 'none' });
		}
		
		if((!this.fault.lightPicUrls || this.fault.lightPicUrls.length === 0) && "SEC" == this.fault.type ){
			return uni.showToast({ title: '请上传逆变器指示灯照片', icon: 'none' });
		}
		
	    const faultId = this.fault.id;
		const params = {
		  id: faultId,
		  faultDesc : this.fault.faultDesc,
		  faultType : this.fault.faultType,
		  faultDeviceChange : this.faultDeviceChange,
		  afterPicUrls : this.fault.afterPicUrls?.map(item => item.url),
		  lightPicUrls : this.fault.lightPicUrls?.map(item => item.url),		  
		};
		uni.showLoading({ title: '正在提交...' });
		const result = await faultApi.finished(params); // 这个 await 也没问题
		uni.hideLoading(); 
		if (result.code === 0) {
		  uni.showToast({ title: '操作成功', icon: 'success' });
		} else {
		  uni.showToast({ title: result.message || '操作失败', icon: 'none' });
		}
		this.loadStationAndFault(Number(faultId));
	},
	
	
	
	async resend() {
		const type = this.fault.type;
		if("FA" == type){
			if (!this.fault.faultLevel) {
			  return uni.showToast({ title: '请选择故障等级', icon: 'none' });
			}
			
			if(!this.fault.beforePicUrls || this.fault.beforePicUrls.length === 0){
				return uni.showToast({ title: '请上传维修前照片', icon: 'none' });
			}
			
			if (!this.fault.faultType) {
			  return uni.showToast({ title: '请选择故障类型', icon: 'none' });
			}
			
			if (this.fault.faultType == 2 ) {
			  // 已经选中
			  if (!Array.isArray(this.faultDeviceChange) || this.faultDeviceChange.length === 0) {
			    return uni.showToast({ title: '请选择更换设备', icon: 'none' });
			  }
			}
			
			if (!this.fault.faultDesc) {
			  return uni.showToast({ title: '请填写故障问题', icon: 'none' });
			}
			
			if(!this.fault.afterPicUrls || this.fault.afterPicUrls.length === 0){
				return uni.showToast({ title: '请上传维修后照片', icon: 'none' });
			}
			
			const faultId = this.fault.id;
			const params = {
			  id: faultId,
			  type : this.fault.type,
			  faultLevel : this.fault.faultLevel,
			  beforePicUrls : this.fault.beforePicUrls?.map(item => item.url),
			  faultDesc : this.fault.faultDesc,
			  faultType : this.fault.faultType,
			  faultDeviceChange : this.faultDeviceChange,
			  afterPicUrls : this.fault.afterPicUrls?.map(item => item.url),
			};
			uni.showLoading({ title: '正在提交...' });
			const result = await faultApi.resend(params); // 这个 await 也没问题
			uni.hideLoading(); 
			if (result.code === 0) {
			  uni.showToast({ title: '操作成功', icon: 'success' });
			} else {
			  uni.showToast({ title: result.message || '操作失败', icon: 'none' });
			}
		}else if("XJ" == type){
			if(!this.fault.comPicUrls || this.fault.comPicUrls.length === 0){
				return uni.showToast({ title: '请上传组件照片', icon: 'none' });
			}
			if(!this.fault.comBackPicUrls || this.fault.comBackPicUrls.length === 0){
				return uni.showToast({ title: '请上传组件背面照片', icon: 'none' });
			}
			if(!this.fault.zhijiaPicUrls || this.fault.zhijiaPicUrls.length === 0){
				return uni.showToast({ title: '请上传支架照片', icon: 'none' });
			}
			if(!this.fault.invPicUrls || this.fault.invPicUrls.length === 0){
				return uni.showToast({ title: '请上传逆变器照片', icon: 'none' });
			}
			if(!this.fault.boxPicUrls || this.fault.boxPicUrls.length === 0){
				return uni.showToast({ title: '请上传交流配电箱照片', icon: 'none' });
			}
			if(!this.fault.elePicUrls || this.fault.elePicUrls.length === 0){
				return uni.showToast({ title: '请上传电表照片', icon: 'none' });
			}
			if(!this.fault.tempPicUrls || this.fault.tempPicUrls.length === 0){
				return uni.showToast({ title: '请上传温度照片', icon: 'none' });
			}
			
			if (!this.fault.faultHas) {
			  return uni.showToast({ title: '请选择是否存在故障', icon: 'none' });
			}
			if (this.fault.faultHas == 'true' && !this.fault.faultDesc ) {
			  return uni.showToast({ title: '请详细描述故障', icon: 'none' });
			}
			
			if (this.fault.faultHas == 'true'  && (!this.fault.beforePicUrls || this.fault.beforePicUrls.length === 0) ) {
			  return uni.showToast({ title: '请上传故障照片', icon: 'none' });
			}
			
			const faultId = this.fault.id;
			const params = {
			  id: faultId,
			  type : this.fault.type,
			  faultHas : this.fault.faultHas,
			  faultDesc : this.fault.faultDesc,
			  comPicUrls : this.fault.comPicUrls?.map(item => item.url),
			  comBackPicUrls : this.fault.comBackPicUrls?.map(item => item.url),
			  zhijiaPicUrls : this.fault.zhijiaPicUrls?.map(item => item.url),
			  invPicUrls : this.fault.invPicUrls?.map(item => item.url),
			  boxPicUrls : this.fault.boxPicUrls?.map(item => item.url),
			  elePicUrls : this.fault.elePicUrls?.map(item => item.url),
			  tempPicUrls : this.fault.tempPicUrls?.map(item => item.url),
			  resiPicUrls : this.fault.resiPicUrls?.map(item => item.url),
			  beforePicUrls : this.fault.beforePicUrls?.map(item => item.url),
			};
			uni.showLoading({ title: '正在提交...' });
			const result = await faultApi.resend(params); // 这个 await 也没问题
			uni.hideLoading(); 
			if (result.code === 0) {
			  uni.showToast({ title: '操作成功', icon: 'success' });
			} else {
			  uni.showToast({ title: result.message || '操作失败', icon: 'none' });
			}
		}else if("SEC" == type){
			if (!this.fault.faultLevel) {
			  return uni.showToast({ title: '请选择故障等级', icon: 'none' });
			}
			
			if(!this.fault.beforePicUrls || this.fault.beforePicUrls.length === 0){
				return uni.showToast({ title: '请上传维修前照片', icon: 'none' });
			}
					
		    if (!Array.isArray(this.faultDeviceChange) || this.faultDeviceChange.length === 0) {
			  return uni.showToast({ title: '请选择更换设备', icon: 'none' });
		    }
			
			if (!this.fault.faultDesc) {
			  return uni.showToast({ title: '请填写故障问题', icon: 'none' });
			}
			
			if(!this.fault.afterPicUrls || this.fault.afterPicUrls.length === 0){
				return uni.showToast({ title: '请上传维修后照片', icon: 'none' });
			}
			
			if((!this.fault.lightPicUrls || this.fault.lightPicUrls.length === 0) && "SEC" == this.fault.type ){
				return uni.showToast({ title: '请上传逆变器指示灯照片', icon: 'none' });
			}
			
			const faultId = this.fault.id;
			const params = {
			  id: faultId,
			  type : this.fault.type,
			  faultLevel : this.fault.faultLevel,
			  beforePicUrls : this.fault.beforePicUrls?.map(item => item.url),
			  faultDesc : this.fault.faultDesc,
			  faultType : this.fault.faultType,
			  faultDeviceChange : this.faultDeviceChange,
			  afterPicUrls : this.fault.afterPicUrls?.map(item => item.url),
			  lightPicUrls : this.fault.lightPicUrls?.map(item => item.url),
			};
			uni.showLoading({ title: '正在提交...' });
			const result = await faultApi.resend(params); // 这个 await 也没问题
			uni.hideLoading(); 
			if (result.code === 0) {
			  uni.showToast({ title: '操作成功', icon: 'success' });
			} else {
			  uni.showToast({ title: result.message || '操作失败', icon: 'none' });
			}
		}else{
			uni.showToast({ title: '运维单类型错误', icon: 'none' });
		}
		
		this.loadStationAndFault(Number(faultId));
	}
	

  }
};
</script>

<style scoped>
.container {
  padding: 30rpx;
  background: #f5f5f5;
}
.card {
  background: #fff;
  border-radius: 16rpx;
  margin-bottom: 20rpx;
  padding: 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}
.card-header {
  font-weight: bold;
  font-size: 30rpx;
  margin-bottom: 16rpx;
}
.card-body view {
  margin-bottom: 10rpx;
}
.photo-section {
  margin-top: 20rpx;
}

.photo-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 12rpx;
  display: flex;
  align-items: center;
}

.img-list {
  display: flex;
  flex-wrap: wrap;
  gap: 12rpx;
}

.preview-img {
  width: 31%;
  height: 180rpx;
  border-radius: 12rpx;
  object-fit: cover;
  box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);
  background-color: #f0f0f0;
}
.text-area {
  width: 70%;
  min-height: 120rpx;
  border: 1rpx solid #ccc;
  border-radius: 8rpx;
  padding: 10rpx;
}
.btn-row {
  margin-top: 10rpx;
}
.submit-btn-wrapper {
  margin-top: 30rpx;
  padding-bottom: 60rpx;
}

.record-item {
  background: #f8f8f8;
  padding: 20rpx;
  border-radius: 12rpx;
}
.record-top {
  display: flex;
  justify-content: space-between;
  font-weight: bold;
  margin-bottom: 8rpx;
}
.result.pass {
  color: green;
}
.result.reject {
  color: red;
}
.record-middle {
  color: #333;
  margin-bottom: 6rpx;
}
.record-bottom {
  font-size: 24rpx;
  color: #999;
}
.form-row {
  display: flex;
  align-items: center;
  margin-bottom: 10rpx;
  width: 100%;
}

.form-label {
  width: 200rpx;
  font-weight: bold;
  color: #333;
}

.picker-box {
  flex: 1;
  padding: 16rpx;
  background-color: #f5f5f5;
  border-radius: 12rpx;
  color: #666;
}

</style>
